package com.ccp.dev.core.basic.web.query;

import com.ccp.dev.core.basic.page.PageBean;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能：对http请求进行封装，用于查询。<br>
 *
 * @author csx
 */
public class QueryFilter extends HashMap {
    private Logger logger = LoggerFactory.getLogger(QueryFilter.class);
    /**
     * 排序列
     */
    public final static String ORDER_FIELD_KEY = "orderField";
    /**
     * 排序方向
     */
    public final static String ORDER_SEQ_KEY = "orderSeq";
    /**
     * 默认排序方向为降序
     */
    public final static String DEFAULT_ORDER_SEQ = "desc";
    /**
     * 分页信息对象
     */
    private PageBean pageBean = null;

    /**
     * 构造QueryFilter，默认为分页处理。
     *
     * @param request
     * @param needPage 是否开启分页
     */
    public QueryFilter(HttpServletRequest request, Boolean needPage) {
        Map<String, Object> map = RequestUtil.getQueryMap(request);
        this.putAll(map);
        // sortname和sortorder是ligerUI自带的前台返回的排序字段和方式名称
        String orderField = RequestUtil.getSecureString(request, "sortName");
        String orderSeq = RequestUtil.getSecureString(request, "sortOrder", DEFAULT_ORDER_SEQ);
        if (StringUtils.isNotEmpty(orderField)) {
            //设置排序信息
            this.setOrderInfo(orderField, orderSeq);
        }
        if (needPage) {
            //开启分页
            Integer page = RequestUtil.getInt(request, PageBean.PAGE_SIZE_NAME, 1);
            Integer pageSize = RequestUtil.getInt(request, PageBean.PAGE_NUMBER_NAME, PageBean.DEFAULT_PAGE_SIZE);
            this.pageBean = new PageBean(page, pageSize);
            this.pageBean.setShowTotal(true);
        }
    }

    /**
     * 构造普通请求的分页的QueryFilter（不适用displaytag列表），这个filter默认为分页。
     *
     * @param request
     */
    public QueryFilter(HttpServletRequest request) {
        this(request, true);
    }


    /**
     * @param request   请求ID
     * @param showTotal 是否统计总行数
     */
    public QueryFilter(HttpServletRequest request, Boolean needPage, Boolean showTotal) {
        this(request, needPage);
        if (!showTotal) {
            this.pageBean.setShowTotal(false);
        }
    }

    /**
     * 设置排序的列，默认为降序
     *
     * @param orderField
     */
    public void setOrderInfo(String orderField, String orderSeq) {

        if (!StringUtil.isEmpty(orderField)) {
            this.put(ORDER_FIELD_KEY, orderField);
            if (StringUtil.isEmpty(orderSeq)) {
                //不指定排序方向，则设置为默认排序方向
                orderSeq = DEFAULT_ORDER_SEQ;
            }
            this.put(ORDER_SEQ_KEY, orderSeq);
        }
    }

    public PageBean getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }

    public void addFilter(String filterName, Object params) {
        this.put(filterName, params);
    }
}
